<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use App\Models\RegUser;
use App\Models\Register;

class BaseController extends Controller
{
    protected $user;
    protected $corpid;
    protected $company;
    protected $roleArray;
    public function __construct()
    {
        if(empty(Auth::user())){
            abort(403,'登录信息错误');
        }
        $user = Auth::user()->comuser();
        if(empty($user)){
            abort(403,'登录信息错误');
        }
        $this->user = !empty($user)?$user:'';
        $this->corpid=$user->corpid;
        $this->company=Company::where("id",$this->corpid)->first();
        $this->roleArray = array('9'=>'总','10'=>'考','11'=>'业','12'=>'奖');
        $com = Company::where('id',$user->corpid)->first();
        if(empty($com)){
            //abort(403,'企业未完成初始化');
        }else{
            if($com->init_index != 2){
                if($_SERVER['REQUEST_URI'] != '/sysadmin/feedback/init' && str_contains('/sysadmin/feedback/init-feedback',$_SERVER['REQUEST_URI']) ){
                    abort(403,"企业月度意见名称未填写完成");
                }
            }
        }
        
    }

    public function formatJson($status='200',$msg='没有更多数据',$data=array(),$redirectUrl = '')
    {
        $result = array(
            'status'        => $status,
            'msg'           => $msg,
            'data'          => $data,
            'redirectUrl'   => $redirectUrl
        );
        return Response::json($result);
    }

    /**
     * 创建文件夹 
     */
    protected function MakeDir($directory)
    {
        if (!is_dir($directory)) {
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
                throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
            }
        } elseif (!is_writable($directory)) {
            throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
        }
    }

}

